package com.local.service;

import com.local.mapper.UserMapper;
import com.local.pojo.Role;
import com.local.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lou ke
 * @version 1.0
 * @since 2020/4/20 16:46
 */
@Service
public class UserService implements UserDetailsService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.loadUserByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("账户不存在！");
        }
        // 获取该用户所拥有的角色
        List<Role> rolse = userMapper.getUserRolesByUid(user.getId());
        user.setRoles(rolse);
        return user;
    }
}
